package com.demo.controller;

import com.demo.common.R;
import com.demo.domain.AddressBook;
import com.demo.service.AddressBookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 地址管理
 */
@Slf4j
@RestController
@RequestMapping("/addressBook")
@Api(tags="地址簿详情")
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;

    /**
     * 新增
     */
    @PostMapping
    @ApiOperation(value = "新增地址")
    public R<AddressBook> addressBookSave(@RequestBody AddressBook addressBook) {
        log.info("addressBook:{}", addressBook);
        return addressBookService.addressBookSave(addressBook);
    }

    /**
     * 设置默认地址
     */
    @PutMapping("default")
    @ApiOperation(value = "设置默认地址")
    public R<AddressBook> setDefault(@RequestBody AddressBook addressBook) {
        log.info("addressBook:{}", addressBook);

        return addressBookService.setDefault(addressBook);
    }

    /**
     * 根据id查询地址
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询地址")
    public R get(@PathVariable Long id) {
        return addressBookService.get(id);
    }

    /**
     * 查询默认地址
     */
    @GetMapping("default")
    @ApiOperation(value = "查询默认地址")
    public R<AddressBook> getDefault() {
        return addressBookService.getDefault();
    }

    /**
     * 查询指定用户的全部地址
     */
    @GetMapping("/list")
    @ApiOperation(value = "查询指定用户的全部地址")
    public R<List<AddressBook>> list(AddressBook addressBook) {
        return addressBookService.list(addressBook);
    }
}
